package org.jim.server.command.handler;

import org.jim.core.ImChannelContext;
import org.jim.core.ImPacket;
import org.jim.core.exception.ImException;
import org.jim.core.packets.Command;
import org.jim.core.packets.HttpReqBody;
import org.jim.core.packets.HttpRespBody;
import org.jim.core.utils.JsonKit;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.processor.HttpCmdProcessor;
import org.jim.server.protocol.ProtocolManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * http处理器
 * @author WChao
 * @date 2018年4月10日 下午2:40:07
 */
public class HttpReqHandler extends AbstractCmdHandler {

	private static Logger log = LoggerFactory.getLogger(HttpReqHandler.class);

	@Override
	public ImPacket handler(ImPacket packet, ImChannelContext imChannelContext) throws ImException {
//		ImServerChannelContext imServerChannelContext = (ImServerChannelContext)imChannelContext;
		HttpReqBody httpReqBody = JsonKit.toBean(packet.getBody(), HttpReqBody.class);
		HttpCmdProcessor httpCmdProcessor = this.getSingleProcessor(HttpCmdProcessor.class);
		int operation = httpReqBody.getOperation();
		HttpRespBody httpRespBody = null;
		switch (operation) {
			case 1:
				httpRespBody = httpCmdProcessor.getUserInfo(httpReqBody, imChannelContext);
				break ;
			case 2:
				httpRespBody = httpCmdProcessor.getConversationsList(httpReqBody, imChannelContext);
				break ;
			case 3:
				httpRespBody = httpCmdProcessor.cancelChatLog(httpReqBody, imChannelContext);
				break ;
			case 4:
				httpRespBody = httpCmdProcessor.clearUnreadCount(httpReqBody, imChannelContext);
				break ;
			case 5:
				httpRespBody = httpCmdProcessor.updateIsPinned(httpReqBody, imChannelContext);
				break ;
			case 6:
				httpRespBody = httpCmdProcessor.clearChatLog(httpReqBody, imChannelContext);
				break ;
			case 7:
				httpRespBody = httpCmdProcessor.clearConversations(httpReqBody, imChannelContext);
				break ;
			case 8:
				httpRespBody = httpCmdProcessor.getChatLogsList(httpReqBody, imChannelContext);
				break ;

		}
		return ProtocolManager.Converter.respPacket(httpRespBody, imChannelContext);
	}

	@Override
	public Command command() {
		return Command.COMMAND_GET_HTTP_REQ;
	}

}
